package com.rowenci.iot_agriculture.service.impl;

import com.rowenci.iot_agriculture.bean.iot.Com;
import com.rowenci.iot_agriculture.bean.iot.Message;
import com.rowenci.iot_agriculture.mapper.ComDao;
import com.rowenci.iot_agriculture.mapper.MessageDao;
import com.rowenci.iot_agriculture.service.api.ComService;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class ComServiceImpl implements ComService {

    @Resource
    private ComDao comDao;
    @Resource
    private MessageDao messageDao;

    @Override
    public boolean addCom(Com com) {

        if (comDao.findComByComName(com.getComName()) != null) {
            return false;
        } else {
            comDao.save(com);
            if (comDao.findComByComName(com.getComName()) != null) {
                return true;
            } else {
                return false;
            }
        }

    }

    @Override
    public List<Com> findAllExceptReader() {
        return comDao.findAllBySensorNot("reader");
    }

    @Override
    public List<Com> findComBySensor(String sensor) {
        if (comDao == null){
            comDao = new ComDao() {
                @Override
                public Com findComByComName(String comName) {
                    return null;
                }

                @Override
                public List<Com> findComsBySensor(String sensor) {
                    List<Com> list = new ArrayList<>();
                    String driver = "com.mysql.cj.jdbc.Driver";
                    String url = "jdbc:mysql://cdb-qhgx4ekn.gz.tencentcdb.com:10000/iot_agriculture?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true";
                    String username = "root";
                    String password = "bb3960519";
                    Connection conn = null;

                    try {
                        Class.forName(driver); //classLoader,加载对应驱动
                        conn = (Connection) DriverManager.getConnection(url, username, password);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                    ResultSet i = null;
                    String sql = "select * from com where sensor = '" + sensor + "'";
                    PreparedStatement pstmt;
                    try {
                        pstmt = (PreparedStatement) conn.prepareStatement(sql);
                        i = pstmt.executeQuery();
                        while (i.next()){
                            Com com = new Com();
                            com.setId(i.getInt("id"));
                            com.setHouseId(i.getInt("house_id"));
                            com.setComName(i.getString("com_name"));
                            com.setBaudrate(i.getInt("baudrate"));
                            com.setSensor(i.getString("sensor"));
                            list.add(com);
                        }
                        pstmt.close();
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    return list;
                }

                @Override
                public List<Com> findAllBySensorNot(String sensor) {
                    return null;
                }

                @Override
                public List<Com> findComsByHouseIdAndSensorIsNot(Integer house_id, String sensor) {
                    return null;
                }

                @Override
                public void deleteComsByHouseId(Integer house_id) {

                }

                @Override
                public List<Com> findComsByHouseId(Integer house_id) {
                    return null;
                }

                @Override
                public Com findComByHouseIdAndSensorIs(Integer houseId, String sensorName) {
                    return null;
                }

                @Override
                public Com findComByHouseId(Integer house_id) {
                    return null;
                }

                @Override
                public Com findComById(Integer id) {
                    return null;
                }

                @Override
                public List<Com> findAll() {
                    return null;
                }

                @Override
                public List<Com> findAll(Sort sort) {
                    return null;
                }

                @Override
                public List<Com> findAllById(Iterable<Integer> iterable) {
                    return null;
                }

                @Override
                public <S extends Com> List<S> saveAll(Iterable<S> iterable) {
                    return null;
                }

                @Override
                public void flush() {

                }

                @Override
                public <S extends Com> S saveAndFlush(S s) {
                    return null;
                }

                @Override
                public void deleteInBatch(Iterable<Com> iterable) {

                }

                @Override
                public void deleteAllInBatch() {

                }

                @Override
                public Com getOne(Integer integer) {
                    return null;
                }

                @Override
                public <S extends Com> List<S> findAll(Example<S> example) {
                    return null;
                }

                @Override
                public <S extends Com> List<S> findAll(Example<S> example, Sort sort) {
                    return null;
                }

                @Override
                public Page<Com> findAll(Pageable pageable) {
                    return null;
                }

                @Override
                public <S extends Com> S save(S s) {
                    return null;
                }

                @Override
                public Optional<Com> findById(Integer integer) {
                    return Optional.empty();
                }

                @Override
                public boolean existsById(Integer integer) {
                    return false;
                }

                @Override
                public long count() {
                    return 0;
                }

                @Override
                public void deleteById(Integer integer) {

                }

                @Override
                public void delete(Com com) {

                }

                @Override
                public void deleteAll(Iterable<? extends Com> iterable) {

                }

                @Override
                public void deleteAll() {

                }

                @Override
                public <S extends Com> Optional<S> findOne(Example<S> example) {
                    return Optional.empty();
                }

                @Override
                public <S extends Com> Page<S> findAll(Example<S> example, Pageable pageable) {
                    return null;
                }

                @Override
                public <S extends Com> long count(Example<S> example) {
                    return 0;
                }

                @Override
                public <S extends Com> boolean exists(Example<S> example) {
                    return false;
                }
            };
        }
        return comDao.findComsBySensor("reader");
    }

    @Override
    public boolean delComById(Integer id) {
        comDao.deleteById(id);
        messageDao.deleteById(messageDao.findByComId(id).getId());
        return true;
    }

    @Override
    public List<Com> findAll() {
        return comDao.findAll();
    }

    @Override
    public List<Com> findAllByHouse_idExceptReader(Integer house_id) {
        return comDao.findComsByHouseIdAndSensorIsNot(house_id, "reader");
    }

    @Override
    public boolean delComByHouse_id(Integer house_id) {
        try{
            comDao.deleteComsByHouseId(house_id);
            messageDao.deleteById(messageDao.findByComId(comDao.findComByHouseId(house_id).getId()).getId());
        }catch (NullPointerException e){
            return true;
        }
        if (!comDao.findComsByHouseId(house_id).isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public Com findComByHouse_idAndSensor(Integer house_id, String sensor) {
        if (comDao == null){
            comDao = new ComDao() {
                @Override
                public Com findComByComName(String comName) {
                    return null;
                }

                @Override
                public List<Com> findComsBySensor(String sensor) {
                    return null;
                }

                @Override
                public List<Com> findAllBySensorNot(String sensor) {
                    return null;
                }

                @Override
                public List<Com> findComsByHouseIdAndSensorIsNot(Integer house_id, String sensor) {
                    return null;
                }

                @Override
                public void deleteComsByHouseId(Integer house_id) {

                }

                @Override
                public List<Com> findComsByHouseId(Integer house_id) {
                    return null;
                }

                @Override
                public Com findComByHouseIdAndSensorIs(Integer houseId, String sensorName) {
                    Com com = new Com();
                    String driver = "com.mysql.cj.jdbc.Driver";
                    String url = "jdbc:mysql://cdb-qhgx4ekn.gz.tencentcdb.com:10000/iot_agriculture?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true";
                    String username = "root";
                    String password = "bb3960519";
                    Connection conn = null;

                    try {
                        Class.forName(driver); //classLoader,加载对应驱动
                        conn = (Connection) DriverManager.getConnection(url, username, password);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                    ResultSet i = null;
                    String sql = "select * from com where house_id = '" + houseId + "' and sensor = '" + sensorName + "'";
                    PreparedStatement pstmt;
                    try {
                        pstmt = (PreparedStatement) conn.prepareStatement(sql);
                        i = pstmt.executeQuery();
                        while (i.next()){
                            com.setId(i.getInt("id"));
                            com.setHouseId(i.getInt("house_id"));
                            com.setComName(i.getString("com_name"));
                            com.setBaudrate(i.getInt("baudrate"));
                            com.setSensor(i.getString("sensor"));
                        }
                        pstmt.close();
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    return com;
                }

                @Override
                public Com findComByHouseId(Integer house_id) {
                    return null;
                }

                @Override
                public Com findComById(Integer id) {
                    Com com = new Com();
                    String driver = "com.mysql.cj.jdbc.Driver";
                    String url = "jdbc:mysql://cdb-qhgx4ekn.gz.tencentcdb.com:10000/iot_agriculture?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true";
                    String username = "root";
                    String password = "bb3960519";
                    Connection conn = null;

                    try {
                        Class.forName(driver); //classLoader,加载对应驱动
                        conn = (Connection) DriverManager.getConnection(url, username, password);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                    ResultSet i = null;
                    String sql = "select * from com where id = '" + id + "'";
                    PreparedStatement pstmt;
                    try {
                        pstmt = (PreparedStatement) conn.prepareStatement(sql);
                        i = pstmt.executeQuery();
                        while (i.next()){
                            com.setId(i.getInt("id"));
                            com.setHouseId(i.getInt("house_id"));
                            com.setComName(i.getString("com_name"));
                            com.setBaudrate(i.getInt("baudrate"));
                            com.setSensor(i.getString("sensor"));
                        }
                        pstmt.close();
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    return com;
                }

                @Override
                public List<Com> findAll() {
                    return null;
                }

                @Override
                public List<Com> findAll(Sort sort) {
                    return null;
                }

                @Override
                public List<Com> findAllById(Iterable<Integer> iterable) {
                    return null;
                }

                @Override
                public <S extends Com> List<S> saveAll(Iterable<S> iterable) {
                    return null;
                }

                @Override
                public void flush() {

                }

                @Override
                public <S extends Com> S saveAndFlush(S s) {
                    return null;
                }

                @Override
                public void deleteInBatch(Iterable<Com> iterable) {

                }

                @Override
                public void deleteAllInBatch() {

                }

                @Override
                public Com getOne(Integer integer) {
                    return null;
                }

                @Override
                public <S extends Com> List<S> findAll(Example<S> example) {
                    return null;
                }

                @Override
                public <S extends Com> List<S> findAll(Example<S> example, Sort sort) {
                    return null;
                }

                @Override
                public Page<Com> findAll(Pageable pageable) {
                    return null;
                }

                @Override
                public <S extends Com> S save(S s) {
                    return null;
                }

                @Override
                public Optional<Com> findById(Integer integer) {
                    return Optional.empty();
                }

                @Override
                public boolean existsById(Integer integer) {
                    return false;
                }

                @Override
                public long count() {
                    return 0;
                }

                @Override
                public void deleteById(Integer integer) {

                }

                @Override
                public void delete(Com com) {

                }

                @Override
                public void deleteAll(Iterable<? extends Com> iterable) {

                }

                @Override
                public void deleteAll() {

                }

                @Override
                public <S extends Com> Optional<S> findOne(Example<S> example) {
                    return Optional.empty();
                }

                @Override
                public <S extends Com> Page<S> findAll(Example<S> example, Pageable pageable) {
                    return null;
                }

                @Override
                public <S extends Com> long count(Example<S> example) {
                    return 0;
                }

                @Override
                public <S extends Com> boolean exists(Example<S> example) {
                    return false;
                }
            };
        }
        return comDao.findComByHouseIdAndSensorIs(house_id, sensor);
    }

    @Override
    public Com findComById(Integer id) {
        if (comDao == null){
            comDao = new ComDao() {
                @Override
                public Com findComByComName(String comName) {
                    return null;
                }

                @Override
                public List<Com> findComsBySensor(String sensor) {
                    return null;
                }

                @Override
                public List<Com> findAllBySensorNot(String sensor) {
                    return null;
                }

                @Override
                public List<Com> findComsByHouseIdAndSensorIsNot(Integer house_id, String sensor) {
                    return null;
                }

                @Override
                public void deleteComsByHouseId(Integer house_id) {

                }

                @Override
                public List<Com> findComsByHouseId(Integer house_id) {
                    return null;
                }

                @Override
                public Com findComByHouseIdAndSensorIs(Integer houseId, String sensorName) {
                    Com com = new Com();
                    String driver = "com.mysql.cj.jdbc.Driver";
                    String url = "jdbc:mysql://cdb-qhgx4ekn.gz.tencentcdb.com:10000/iot_agriculture?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true";
                    String username = "root";
                    String password = "bb3960519";
                    Connection conn = null;

                    try {
                        Class.forName(driver); //classLoader,加载对应驱动
                        conn = (Connection) DriverManager.getConnection(url, username, password);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                    ResultSet i = null;
                    String sql = "select * from com where house_id = '" + houseId + "' and sensor = '" + sensorName + "'";
                    PreparedStatement pstmt;
                    try {
                        pstmt = (PreparedStatement) conn.prepareStatement(sql);
                        i = pstmt.executeQuery();
                        while (i.next()){
                            com.setId(i.getInt("id"));
                            com.setHouseId(i.getInt("house_id"));
                            com.setComName(i.getString("com_name"));
                            com.setBaudrate(i.getInt("baudrate"));
                            com.setSensor(i.getString("sensor"));
                        }
                        pstmt.close();
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    return com;
                }

                @Override
                public Com findComByHouseId(Integer house_id) {
                    return null;
                }

                @Override
                public Com findComById(Integer id) {
                    Com com = new Com();
                    String driver = "com.mysql.cj.jdbc.Driver";
                    String url = "jdbc:mysql://cdb-qhgx4ekn.gz.tencentcdb.com:10000/iot_agriculture?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true";
                    String username = "root";
                    String password = "bb3960519";
                    Connection conn = null;

                    try {
                        Class.forName(driver); //classLoader,加载对应驱动
                        conn = (Connection) DriverManager.getConnection(url, username, password);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                    ResultSet i = null;
                    String sql = "select * from com where id = '" + id + "'";
                    PreparedStatement pstmt;
                    try {
                        pstmt = (PreparedStatement) conn.prepareStatement(sql);
                        i = pstmt.executeQuery();
                        while (i.next()){
                            com.setId(i.getInt("id"));
                            com.setHouseId(i.getInt("house_id"));
                            com.setComName(i.getString("com_name"));
                            com.setBaudrate(i.getInt("baudrate"));
                            com.setSensor(i.getString("sensor"));
                        }
                        pstmt.close();
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    return com;
                }

                @Override
                public List<Com> findAll() {
                    return null;
                }

                @Override
                public List<Com> findAll(Sort sort) {
                    return null;
                }

                @Override
                public List<Com> findAllById(Iterable<Integer> iterable) {
                    return null;
                }

                @Override
                public <S extends Com> List<S> saveAll(Iterable<S> iterable) {
                    return null;
                }

                @Override
                public void flush() {

                }

                @Override
                public <S extends Com> S saveAndFlush(S s) {
                    return null;
                }

                @Override
                public void deleteInBatch(Iterable<Com> iterable) {

                }

                @Override
                public void deleteAllInBatch() {

                }

                @Override
                public Com getOne(Integer integer) {
                    return null;
                }

                @Override
                public <S extends Com> List<S> findAll(Example<S> example) {
                    return null;
                }

                @Override
                public <S extends Com> List<S> findAll(Example<S> example, Sort sort) {
                    return null;
                }

                @Override
                public Page<Com> findAll(Pageable pageable) {
                    return null;
                }

                @Override
                public <S extends Com> S save(S s) {
                    return null;
                }

                @Override
                public Optional<Com> findById(Integer integer) {
                    return Optional.empty();
                }

                @Override
                public boolean existsById(Integer integer) {
                    return false;
                }

                @Override
                public long count() {
                    return 0;
                }

                @Override
                public void deleteById(Integer integer) {

                }

                @Override
                public void delete(Com com) {

                }

                @Override
                public void deleteAll(Iterable<? extends Com> iterable) {

                }

                @Override
                public void deleteAll() {

                }

                @Override
                public <S extends Com> Optional<S> findOne(Example<S> example) {
                    return Optional.empty();
                }

                @Override
                public <S extends Com> Page<S> findAll(Example<S> example, Pageable pageable) {
                    return null;
                }

                @Override
                public <S extends Com> long count(Example<S> example) {
                    return 0;
                }

                @Override
                public <S extends Com> boolean exists(Example<S> example) {
                    return false;
                }
            };
        }
        return comDao.findComById(id);
    }

}
